// pages/login/index.js
const app = getApp()
Page({

     /**
      * 页面的初始数据
      */
     data: {
          mobile: '',
          send_time: 0, //再次获取验证码时间 倒计时
          send_count: 1, //第一次发送验证码
          userInfo: {},
          code_id: '', //短信验证码存在数据库的id
          kefu: false,
          isAuthorize: true, //是否已经授权登录
          invi_uid: '', //邀请人的用户ID
          setting: {}
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          wx.setNavigationBarTitle({
               title: '绑定手机号'
          })
          
          let that = this;
          //设置
          app.connect('index/setting', {}, function (res) {
               that.setData({
                    setting: res
               });
          });
          //邀请人的用户ID
          var invi_uid = options.invi_uid;
          that.setData({
               invi_uid: invi_uid
          });
          //判断有没有授权登录
          wx.getSetting({
               success(res) {
                    //没有授权
                    if (!res.authSetting['scope.userInfo']) {
                         that.setData({
                              isAuthorize: false
                         });

                    } else {
                         that.setData({
                              isAuthorize: true
                         });
                         console.log('已经授权');
                         app.getUserInfo(function (userInfo) {
                              that.setData({
                                   userInfo: userInfo.data
                              });
                              //校验是否绑定了手机号
                              if (userInfo.data.mobile != '0') {
                                   wx.showModal({
                                        title: '提示',
                                        content: '您已经是注册用户，欢迎使用',
                                        showCancel: false,
                                        confirmText: "进入首页",
                                        success: function (res) {
                                             if (res.confirm) {
                                                  wx.reLaunch({
                                                       url: '../index/index'
                                                  })
                                                  return false;
                                             }
                                        }
                                   })
                              } else {
                                   //更新邀请人uid
                                   app.connect('account/setInviUid', {
                                        uid: that.data.userInfo.uid,
                                        openid: that.data.userInfo.openId,
                                        invi_uid: that.data.invi_uid
                                   }, function (res) {
                                        if (res.code == 1) {
                                             console.log('更新邀请人uid成功');
                                        } else {
                                            console.log(res.msg);
                                        }
                                   });
                              }
                         })
                    }
               }
          })
     },
     getuserinfo: function (options) {
          let that = this;
          wx.showLoading({
               title: '加载中...',
          })
          wx.getSetting({
               success(res) {
                    //授权
                    if (res.authSetting['scope.userInfo']) {
                         that.setData({
                              isAuthorize: true
                         });
                         app.getUserInfo(function (userInfo) {
                              //检查是不是新注册的 通过检验有没有注册过手机号
                              if (userInfo.data.mobile != '0') {
                                   wx.showModal({
                                        title: '提示',
                                        content: '您已经是注册用户，欢迎使用',
                                        showCancel: false,
                                        confirmText: "进入首页",
                                        success: function (res) {
                                             if (res.confirm) {
                                                  wx.reLaunch({
                                                       url: '../index/index'
                                                  })
                                                  return false;
                                             }
                                        }
                                   })
                              } else {

                                   //更新数据
                                   that.setData({
                                        userInfo: userInfo.data
                                   })

                                   //更新邀请人uid
                                   app.connect('account/setInviUid', {
                                        uid: that.data.userInfo.uid,
                                        openid: that.data.userInfo.openId,
                                        invi_uid: that.data.invi_uid
                                   }, function (res) {
                                        if (res.code == 1) {
                                             console.log('更新邀请人uid成功');
                                            
                                        } else {
                                            
                                        }
                                  
                                   });
                              
                                 
                              }
                         })
                    } else {
                         wx.showToast({
                              title: '抱歉！您没有授权本应用，无法进行下一步操作',
                              icon: 'none'
                         })
                         wx.hideLoading()
                    }
               }
          })

     },
     getuserinfo1: function (options) {
          let that = this;
          wx.showLoading({
               title: '加载中...',
          })
          wx.getSetting({
               success(res) {
                    //授权
                    if (res.authSetting['scope.userInfo']) {
                         that.setData({
                              isAuthorize: true
                         });
                         app.getUserInfo(function (userInfo) {
                              //检查是不是新注册的 通过检验有没有注册过手机号
                              if (userInfo.data.mobile != '0') {
                                   wx.showModal({
                                        title: '提示',
                                        content: '您已经是注册用户，欢迎使用',
                                        showCancel: false,
                                        confirmText: "进入首页",
                                        success: function (res) {
                                             if (res.confirm) {
                                                  wx.reLaunch({
                                                       url: '../index/index'
                                                  })
                                                  return false;
                                             }
                                        }
                                   })
                              } else {

                                   //更新数据
                                   that.setData({
                                        userInfo: userInfo.data
                                   })

                                   //更新邀请人uid
                                   app.connect('account/setInviUid', {
                                        uid: that.data.userInfo.uid,
                                        openid: that.data.userInfo.openId,
                                        invi_uid: that.data.invi_uid
                                   }, function (res) {
                                        if (res.code == 1) {
                                             console.log('更新邀请人uid成功');

                                        } else {

                                        }
                                        wx.switchTab({
                                             url: '../index/index'
                                        })
                                   });


                              }
                         })
                    } else {
                         wx.showToast({
                              title: '抱歉！您没有授权本应用，无法进行下一步操作',
                              icon: 'none'
                         })
                         wx.hideLoading()
                    }
               }
          })

     },
     /**
      * 获取手机验证码
      */
     getCode: function (e) {

          let that = this;
          let mobile = that.data.mobile;
          if (!app.isPoneAvailable(mobile)) {
               wx.showToast({
                    title: '不是有效的手机号',
                    icon: 'none'
               })
               return false;
          }
          //检测手机号是否被占用，如果占用不能继续注册
          app.connect('login/checkMobile', {
               openid: that.data.userInfo.openId,
               uid: that.data.userInfo.uid,
               mobile: mobile
          }, function (res) {
               console.log(res);
               //被占用 提示
               if (res.code != 1) {
                    wx.showToast({
                         title: '手机号已被占用',
                         icon: 'none'
                    })
               } else {
                    //获取验证码按钮更改样式
                    that.setData({
                         send_time: 60
                    })
                    var referId = setInterval(function () {
                         if (that.data.send_time == 0) {
                              clearInterval(referId);
                              return false;
                         }
                         if (that.data.send_time == 1) {
                              that.setData({
                                   send_count: that.data.send_count + 1
                              })
                         }
                         that.setData({
                              send_time: that.data.send_time - 1
                         })
                    }, 1000);
                    //发送验证码  
                    app.connect('login/sendCode', {
                         openid: that.data.userInfo.openId,
                         uid: that.data.userInfo.uid,
                         mobile: mobile
                    }, function (res) {
                         console.log(res);
                         if (res.code_id != undefined) {
                              that.setData({
                                   code_id: res.code_id
                              });
                              wx.showToast({
                                   title: '验证码发送成功'
                              })
                         } else {
                              wx.showToast({
                                   title: res.msg,
                                   icon: 'none'
                              })
                              console.log(res);
                         }
                    });
               }
          })
     },
     //手机号输入监听
     input: function (e) {
          this.setData({
               mobile: e.detail.value
          });
     },
     submit: function (e) {
          console.log(e);
          var code = e.detail.value.code;
          let that = this;
          //校验验证码是否正确
          app.connect('login/checkCode', {
               uid: that.data.userInfo.uid,
               openid: that.data.userInfo.openId,
               code_id: that.data.code_id,
               mobile: that.data.mobile,
               supe_uid: that.data.invi_uid,
               code: code
          }, function (res) {
               console.log(res);

               if (res.code == 1) {
                    //验证成功 进行下一步操作
                    app.login();
                    wx.showToast({
                         title: '绑定手机号成功'
                    })
                    wx.reLaunch({
                         url: '../my/index',
                    })
               } else {
                    //
                    wx.showToast({
                         title: res.msg,
                         icon: 'none'
                    })
               }
          });
     },

})